﻿Imports System.Data.SqlClient
Imports DTO

Public Class TonKhoDAO
    'HuyBinh
    Public Shared Function LoadHangTonKho() As DataTable
        Return CKetNoi.LoadBang("usp_HangTonKho")
    End Function
    'HuyBinh
    Public Shared Function LoadBangKhoHangTheoMa(maKhoHang As String) As DataTable
        Dim tenStore As String = "usp_HangTonKhoTheoMaKho"
        Dim dsThamSo As New List(Of SqlParameter)
        dsThamSo.Add(New SqlParameter("@MaKhoHang", maKhoHang))
        Return CKetNoi.ThucThiStoreCoThamSo(tenStore, dsThamSo)
    End Function

    'HuyBinh
    Public Shared Function LayMangHangHoaTrongKho() As List(Of TonKhoDTO)
        Dim dsHangTonKho As New List(Of TonKhoDTO)
        Dim tenStore = "usp_LoadHangHoaTrongKho"
        Dim cmd As New SqlCommand(tenStore, CKetNoi.Connect)
        CKetNoi.Connect.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader
        While dr.Read()
            Dim hangTonKho As New TonKhoDTO
            hangTonKho.MaHangHoa = dr("MaHangHoa")
            hangTonKho.TenHangHoa = dr("TenHangHoa")
            hangTonKho.SoLuongTon = dr("SoLuongTon")
            hangTonKho.Gia = dr("GiaMua")
            hangTonKho.TenKhoHang = dr("TenKhoHang")
            hangTonKho.DonVi = dr("TenDonVi")
            hangTonKho.MaKhoMacDinh = dr("MaKhoMacDinh")
            dsHangTonKho.Add(hangTonKho)
        End While
        CKetNoi.Connect.Close()
        Return dsHangTonKho
    End Function

    'HuyBinh
    Public Shared Function CapNhatSLHangTonKhoDi(hangTonKho As CTChuyenKhoDTO) As Integer
        Dim dsThamSo As New List(Of SqlParameter)
        dsThamSo.Add(New SqlParameter("@MaHangHoa", hangTonKho.MaHangHoa))
        dsThamSo.Add(New SqlParameter("@MaKhoHang", hangTonKho.TuKho))
        dsThamSo.Add(New SqlParameter("@SoLuong", hangTonKho.SoLuong))
        Dim kq As Integer = CKetNoi.ExecQueryStoreProc("usp_CapNhatSLHangTonKhoDi", dsThamSo)
        Return kq
    End Function

    Public Shared Function CapNhatSLHangTonKhoDen(hangTonKho As CTChuyenKhoDTO) As Integer
        Dim dsThamSo As New List(Of SqlParameter)
        dsThamSo.Add(New SqlParameter("@MaHangHoa", hangTonKho.MaHangHoa))
        dsThamSo.Add(New SqlParameter("@MaKhoHang", hangTonKho.DenKho))
        dsThamSo.Add(New SqlParameter("@SoLuong", hangTonKho.SoLuong))
        Dim kq As Integer = CKetNoi.ExecQueryStoreProc("usp_CapNhatSLHangTonKhoDen", dsThamSo)
        Return kq
    End Function

    Public Shared Function ThemHangTonKho(hangTonKho As CTChuyenKhoDTO) As Integer
        Dim dsThamSo As New List(Of SqlParameter)
        dsThamSo.Add(New SqlParameter("@MaHangHoa", hangTonKho.MaHangHoa))
        dsThamSo.Add(New SqlParameter("@MaKhoHang", hangTonKho.DenKho))
        dsThamSo.Add(New SqlParameter("@SoLuong", hangTonKho.SoLuong))
        Dim kq As Integer = CKetNoi.ExecQueryStoreProc("usp_ThemHangTonKho", dsThamSo)
        Return kq
    End Function
End Class
